package com.anxin.service.impl;

import com.anxin.entity.MedicalRecord;
import com.anxin.mapper.MedicalRecordMapper;
import com.anxin.service.MedicalRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;

/**
 * 病历记录服务实现类
 */
@Service
public class MedicalRecordServiceImpl extends ServiceImpl<MedicalRecordMapper, MedicalRecord> implements MedicalRecordService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createMedicalRecord(MedicalRecord medicalRecord) {
        this.save(medicalRecord);
    }

    @Override
    public List<MedicalRecord> getDoctorMedicalRecords(String doctorId, LocalDate date) {
        return this.list(new LambdaQueryWrapper<MedicalRecord>()
                .eq(MedicalRecord::getDoctorId, doctorId)
                .eq(MedicalRecord::getVisitDate, date)
                .orderByDesc(MedicalRecord::getCreateTime));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMedicalRecord(MedicalRecord medicalRecord) {
        this.updateById(medicalRecord);
    }

    @Override
    public Page<MedicalRecord> page(int pageNum, int pageSize, String patientId, String doctorId, String departmentId) {
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(patientId)) {
            wrapper.eq(MedicalRecord::getPatientId, patientId);
        }
        if (StringUtils.hasText(doctorId)) {
            wrapper.eq(MedicalRecord::getDoctorId, doctorId);
        }
        if (StringUtils.hasText(departmentId)) {
            wrapper.eq(MedicalRecord::getDepartmentId, departmentId);
        }

        // 按创建时间倒序排序
        wrapper.orderByDesc(MedicalRecord::getCreateTime);

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public List<MedicalRecord> getByPatientId(String patientId) {
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MedicalRecord::getPatientId, patientId)
               .orderByDesc(MedicalRecord::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<MedicalRecord> getByDoctorId(String doctorId) {
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MedicalRecord::getDoctorId, doctorId)
               .orderByDesc(MedicalRecord::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<MedicalRecord> getByDepartmentId(String departmentId) {
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MedicalRecord::getDepartmentId, departmentId)
               .orderByDesc(MedicalRecord::getCreateTime);
        return list(wrapper);
    }
}
